package com.seagull.sb.dao.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.seagull.sb.dao.LeaveDAO;
import com.seagull.sb.domain.Leave;
import com.seagull.sb.util.SBRequestContext;

public class LeaveDAOImpl extends BaseDAO implements LeaveDAO {

	@Override
	public List<Leave> getLeaves(SBRequestContext rc) throws Exception {
		Map<String,Object> qmap = new HashMap<String, Object>();
		
		qmap.put("org_id", new Long(rc.getOrgId()));
		
		if(rc.getClassId()>0){
			qmap.put("class_id", new Long(rc.getClassId()));
		}
		Object result = sqlMap.queryForList("leaveDB.getLeaves",qmap);
		if(result instanceof List){
			return (List<Leave>)result;
		}
		return Collections.EMPTY_LIST;
	}


	@Override
	public boolean create(SBRequestContext rc, Leave leave) throws Exception {
		boolean result = false;
		
		Map<String,Object> qmap = new HashMap<String, Object>();
		
		qmap.put("org_id", new Long(rc.getOrgId()));
		qmap.put("class_id", new Long(rc.getClassId()));
		qmap.put("student_id", new Long(rc.getStudentId()));
		
		qmap.put("subject", leave.getSubject()); 
		qmap.put("startdt",  leave.getStartdt());
		qmap.put("enddt",  leave.getEnddt());
		
		Integer id = (Integer) sqlMap.insert("leaveDB.create",qmap);
		if(null != id) { 
			leave.setId(id.intValue());
			System.out.println("Leave Id Save : "+id.toString());
		}
		
		result = true;		
		
		return result;
	}

	@Override
	public boolean update(SBRequestContext rc, Leave leave) throws Exception {
		
		boolean result = false;
		
		Map<String,Object> qmap = new HashMap<String, Object>();
		
		//qmap.put("teacher_id", new Long(rc.getTeacherId()));
		qmap.put("modifiedby", new Long(rc.getTeacherId()));
		
		qmap.put("approved_status", leave.getApproved());
		qmap.put("rejected_comments", leave.getRejectedComments());
		qmap.put("modifieddt", leave.getModifieddt());
		    	
		qmap.put("id", new Long(leave.getId()));
		qmap.put("org_id", new Long(leave.getOrgId()));
		qmap.put("class_id", new Long(leave.getClassId()));
		qmap.put("student_id", new Long(leave.getStudentId()));
		
		Integer id = (Integer) sqlMap.update("leaveDB.update",qmap);

		System.out.println("Leave Id update : "+id.toString());
		result = true;		
		
		return result;
	}
	
}
